<template>
  <!--勘察规范信息新增弹层start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    class="dialogStyleSmall"
    :before-close="cancel"
  >
    <el-form
      v-if="dialogVisible"
      ref="addForm"
      class="dialog-form"
      :model="formData"
      :rules="rules"
      label-width="auto"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="规范名称" prop="standardName">
            <el-input v-model="formData.standardName" placeholder="请输入规范名称" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="规范编号" prop="standardCode">
            <el-input v-model="formData.standardCode" placeholder="请输入规范编号" size="medium" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件" prop="attachIdList">
            <template slot="label">
              <span class="labelTitle" v-html="`附&emsp;&emsp;件`" />
            </template>
            <FileUplodeOne
              ref="fileUplode"
              file-type=".pdf"
              :edit-file-list="FileList"
              @getFileIdsList="getFileList"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <DialogFooter />
    </div>
  </el-dialog>
  <!--试验单位信息新增弹层end -->
</template>

<script>
// 页面接口
import { addStandard, updateStandard, getStandardById } from '@/api/system/standard/index'
// 附件上传
import FileUplodeOne from '@/components/Upload/FileUplodeOne'
// dialog-footer
import DialogFooter from '@/components/Common/Btns/DialogFooter/index'
export default {
  components: {
    FileUplodeOne,
    DialogFooter
  },
  data() {
    return {
      /* 新增 start*/
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      btnDisabled: false, // 确定按钮
      formData: { // 新增数据
        standardName: '', // 规范名称
        standardCode: '', // 规范编号
        attachIdList: ''// 附件
      },
      // 表单验证规则
      rules: {
        standardName: [
          { required: true, message: '规范名称不能为空', pattern: '[^\x22]+', trigger: 'blur' }
        ],
        standardCode: [
          { required: true, message: '规范编号不能为空', trigger: 'blur' },
          { required: true, max: 20, message: '规范编号长度不得超过 20 个字符' }
        ],
        attachIdList: [
          { required: true, message: '请上传附件', trigger: 'blur' }
        ]
      },
      /* 新增 end*/
      FileList: []// 附件

    }
  },
  methods: {
    /* 新增/编辑弹层 start*/
    // 新增/编辑
    handleAdd(title, id) {
      this.dialogTitle = title
      this.dialogVisible = true
      this.emptyAddForm() // 清空勘察单位
      console.log(id)
      if (id) {
        this.getDetailById(id)
      }
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
      this.emptyAddForm()
    },
    // 提交按钮
    submitForm() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          if (this.formData.prosepctId) {
            // 更新
            updateStandard(this.formData).then(res => {
              if (res) {
                this.emptyAddForm()
                this.dialogVisible = false
                this.msgSuccess('修改成功')
                this.$parent.getList()
                this.btnDisabled = false
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          } else {
            addStandard(this.formData).then(res => {
              if (res) {
                this.emptyAddForm()
                this.msgSuccess('保存成功')
                this.dialogVisible = false
                this.$parent.getList()
                this.btnDisabled = false
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          }
        }
      })
    },
    // 获取详情
    getDetailById(id) {
      getStandardById(id).then(res => {
        if (res) {
          this.formData = { ...res.data }
          if (res.data.attachIdList && res.data.attachIdList.length > 0) {
            this.FileList = res.data.attachIdList
          }
        }
      })
    },
    // 表单重置
    emptyAddForm() {
      this.resetForm('addForm')
      this.formData.id = ''
      this.FileList = []
    },
    /* 新增弹层 end*/

    /** ************ 文件上传start *******************/
    // 接受文件上传 组件 回传过来的文件id
    getFileList(fileIdsList) {
      this.formData.attachIdList = fileIdsList
    }
    /** ************ 文件上传end *******************/
  }

}
</script>

<style>

</style>
